package com.manfred.JNDIStudy.search;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;

/**
 * Demonstrates how to perform a search by specifying a search filter and
 * default search controls. Functionally identical to SearchRetAll.java.
 *
 * usage: java SearchWithFilterRetAll
 */
class SearchWithFilterRetAll {
	public static void main(String[] args) {

		// Set up the environment for creating the initial context
		Hashtable<String, Object> env = new Hashtable<String, Object>(11);
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://localhost:10389/o=JNDITutorial");

		try {
			// Create initial context
			DirContext ctx = new InitialDirContext(env);

			// Create default search controls
			SearchControls ctls = new SearchControls();

			// Specify the search filter to match
			// Ask for objects with attribute sn == Smith and which have
			// the "mail" attribute.
			String filter = "(|(&(sn=Dubin)(mail=*))(sn=S*))";

			// Search for objects using filter
			NamingEnumeration answer = ctx.search("ou=People", filter, ctls);

			// Print the answer
			Search.printSearchEnumeration(answer);

			// Close the context when we're done
			ctx.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}